package com.diaohw.module.system.service.impl;

import com.diaohw.module.system.mapper.DepartmentMapper;
import com.diaohw.module.system.obj.dto.DepartmentDto;
import com.diaohw.module.system.obj.entity.Department;
import com.diaohw.module.system.obj.mapstruct.DepartmentMapstruct;
import com.diaohw.module.system.obj.query.DepartmentQuery;
import com.diaohw.module.system.obj.vo.DepartmentTreeVo;
import com.diaohw.module.system.obj.vo.DepartmentVo;
import com.diaohw.module.system.service.DepartmentService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.common.util.TreeUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 部门 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2024-07-05
 */
@Service
public class DepartmentImpl implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public DepartmentVo create(DepartmentDto data) {
        Department model = DepartmentMapstruct.INSTANCE.dtoToEntity(data);
        departmentMapper.insert(model);
        return DepartmentMapstruct.INSTANCE.entityToVo(model);
    }

    @Override
    public DepartmentVo update(String id, DepartmentDto data) {
        Department model = DepartmentMapstruct.INSTANCE.dtoToEntity(data);
        model.setId(id);
        departmentMapper.update(model);
        return DepartmentMapstruct.INSTANCE.entityToVo(model);

    }

    @Override
    public Boolean delete(String id) {
        return departmentMapper.deleteById(id) > 0;
    }

    @Override
    public DepartmentVo detail(String id) {
        Department model = departmentMapper.selectOneById(id);
        return DepartmentMapstruct.INSTANCE.entityToVo(model);

    }

    @Override
    public List<DepartmentVo> findByList(DepartmentQuery query) {
        QueryWrapper wrapper = DepartmentMapper.createWrapper(query);
        List<Department> list = departmentMapper.selectListByQuery(wrapper);
        return DepartmentMapstruct.INSTANCE.entityToVo(list);

    }

    @Override
    public PageResultVo<DepartmentVo> findByPage(DepartmentQuery query) {
        QueryWrapper wrapper = DepartmentMapper.createWrapper(query);
        Page<Department> page = departmentMapper.paginate(query.getPageIndex(), query.getPageSize(), wrapper);
        List<Department> records = page.getRecords();
        return PageResultVo.create(page.getTotalRow(), DepartmentMapstruct.INSTANCE.entityToVo(records));
    }


    @Override
    public List<DepartmentTreeVo> tree() {
        List<Department> list = departmentMapper.selectListByQuery(QueryWrapper.create());

        List<DepartmentTreeVo> vos = list.stream().map(DepartmentMapstruct.INSTANCE::entityToTreeVo)
                .toList();

        return TreeUtil.listToTree(vos, DepartmentTreeVo::getId, DepartmentTreeVo::getParentId, DepartmentTreeVo::getChildren);
    }

}
